/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.text.actions;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.kommando.core.action.ActionExecutionException;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.text.actions.support.TextAction;

/**
 * Display text in a dialog.
 * 
 * @author Peter De Bruycker
 */
public class DisplayDialogAction extends TextAction {

    private static final String ID = "text:display:dialog";
    private Icon icon;

    @Override
    public String getDescription() {
        return "Display the text in a dialog";
    }

    @Override
    public Icon getIcon() {
        if (icon == null) {
            icon = new ImageIcon(getClass().getResource("/icons/window-new_256x256x32.png"));
        }

        return icon;
    }

    @Override
    public String getId() {
        return ID;
    }

    @Override
    public String getName() {
        return "Display Dialog";
    }

    @Override
    protected CatalogObject execute(final String text, CatalogObject indirect) throws ActionExecutionException {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                /*
                 * We cannot use JOptionPane.showMessageDialog method because that method creates a modal dialog, and this prevents the user from using Kommando
                 * until the modal dialog is closed.
                 */
                JOptionPane optionPane = new JOptionPane(text, JOptionPane.INFORMATION_MESSAGE);
                JDialog dialog = optionPane.createDialog("Kommando");
                dialog.setModal(false);
                dialog.setVisible(true);
            }
        });

        return null;
    }

    @Override
    public boolean isApplicable(CatalogObject direct) {
        return direct.hasExtension(String.class);
    }

}
